package org.zsk.cs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zsk.cs.common.AppUtils;
import org.zsk.cs.dao.IdentityAuthDao;
import org.zsk.cs.dao.NoticeDao;
import org.zsk.cs.dao.UserDao;
import org.zsk.cs.domain.po.IdentityAuth;
import org.zsk.cs.domain.po.Notice;
import org.zsk.cs.domain.po.User;
import org.zsk.cs.domain.vo.NoticeVo;
import org.zsk.cs.service.NoticeService;
import org.zsk.cs.util.UserUtils;
import org.zsk.fastdev.assign.Assign;

import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 公告 serviceImpl
 * </p>
 *
 * @author zsk
 * @version 1.0
 * Created on 2020/03/26
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class NoticeServiceImpl extends ServiceImpl<NoticeDao,Notice> implements NoticeService {

    private final UserDao userDao;
    private final NoticeDao noticeDao;
    private final IdentityAuthDao identityAuthDao;
    private final Assign assign;
    private final AppUtils appUtils;

    public NoticeServiceImpl(UserDao userDao, NoticeDao noticeDao, IdentityAuthDao identityAuthDao, Assign assign, AppUtils appUtils) {
        this.userDao = userDao;
        this.noticeDao = noticeDao;
        this.identityAuthDao = identityAuthDao;
        this.assign = assign;
        this.appUtils = appUtils;
    }

    @Override
    public Page<NoticeVo> getWithUserNameByPage(NoticeVo noticeVo) throws Exception {
        Page<Notice> page=new Page<>();
        assign.assign(noticeVo,page,"$current,$size");
        Page<Notice> noticePage=page(page);
        return getNoticeVoPage(noticePage);
    }

    @Override
    public Page<NoticeVo> districtHeadGetWithUserNameByPage(NoticeVo noticeVo) throws Exception {
        IdentityAuth districtHead=appUtils.getDistrictHead();
        return getNoticeVoPageByDistrictHead(noticeVo, districtHead);
    }

    private Page<NoticeVo> getNoticeVoPageByDistrictHead(NoticeVo noticeVo, IdentityAuth districtHead) throws Exception {
        Page<Notice> page = new Page<>();
        assign.assign(noticeVo, page, "$current,$size");
        //获取社区公告
        Page<Notice> noticePage = page(page, new LambdaQueryWrapper<Notice>()
                .eq(Notice::getDistrictHeadId, districtHead.getId()));
        return getNoticeVoPage(noticePage);
    }

    @Override
    public boolean districtHeadSave(NoticeVo noticeVo) throws Exception {
        //获取当前用户的社区长身份
        IdentityAuth districtHead=appUtils.getDistrictHead();
        Notice notice=new Notice();
        assign.assign(noticeVo,notice,"$content");
        notice.setDistrictHeadId(districtHead.getId());
        return save(notice);
    }

    private Page<NoticeVo> getNoticeVoPage(Page<Notice> noticePage) throws Exception {
        Page<NoticeVo> noticeVoPage=new Page<>();
        //合并搜索结果
        List<NoticeVo> noticeVoList=noticePage.getRecords().stream().collect(LinkedList::new,(noticeVos, notice) -> {
            IdentityAuth districtHead=identityAuthDao.selectById(notice.getDistrictHeadId());
            User user=userDao.selectById(districtHead.getUid());
            NoticeVo curNoticeVo=new NoticeVo();
            try {
                assign.assign(notice,curNoticeVo);
                curNoticeVo.setUsername(user.getUsername());
                noticeVos.add(curNoticeVo);
            } catch (Exception e) {
                e.printStackTrace();
            }
        },LinkedList::addAll);
        assign.assign(noticePage,noticeVoPage);
        noticeVoPage.setRecords(noticeVoList);
        return noticeVoPage;
    }

    @Override
    public Page<NoticeVo> residentGetWithUserNameByPage(NoticeVo noticeVo) throws Exception {
        //获取当前用户所在社区的社区长身份
        IdentityAuth districtHead=identityAuthDao.selectById(
                identityAuthDao.selectDistrictHeadIdByResidentId(
                        appUtils.getCurResident().getId()));
        return getNoticeVoPageByDistrictHead(noticeVo, districtHead);
    }
}
